package com.app.business.web.business.newTextForCs;

import com.app.business.model.newTextForCs.TextHouseHoldAndArea;
import com.app.business.model.newTextForCs.TextParameter;
import com.app.business.service.newTextForCs.TextHouseHoldAndAreaService;
import com.app.business.service.newTextForCs.TextParameterService;
import com.app.system.jpa.PageBean;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.AjaxException;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 测算基础信息控制层
 * Created by ddz on 2020/1/20.
 */
@Controller
@RequestMapping("/business/newTextForCs/textParameter/*")
@SuppressWarnings("all")
public class TextParameterController {
    @Autowired
    private TextParameterService textParameterService;
    @Autowired
    private TextHouseHoldAndAreaService textHouseHoldAndAreaService;

    /**
     * 参数配置项目数据列表
     */
    @RequestMapping("list")
    @RequiresPermissions("textParameter:list")
    public void list() {
    }

    /**
     * 加载征收参数配置数据
     *
     * @return
     */
    @RequestMapping("loadTextParameterList")
    @ResponseBody
    @RequiresPermissions("textParameter:loadTextParameterList")
    public Map loadTextParameterList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<TextParameter> textParameterPageBean = this.textParameterService.getTextParameterPageBean();
        List<TextParameter> textParameterList = textParameterPageBean.getDatas();
        if (!CollectionUtils.isEmpty(textParameterList)) {
            Map<String, Object> item = null;
            for (TextParameter textParameter : textParameterList) {
                item = Maps.newHashMap();
                item.put("id", textParameter.getId());
                item.put("parameterCode", StringUtils.getNotNull(textParameter.getParameterCode()));
                item.put("parameterName", StringUtils.getNotNull(textParameter.getParameterName()));
                listData.add(item);
            }
        }
        return LayUiUtils.page(textParameterPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 数据配置表单
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("dataForm")
    @RequiresPermissions("textParameter:dataForm")
    public void dataForm(HttpServletRequest request, ModelMap modelMap) {
        int textParameterId = ParamUtils.getInt(request, "textParameterId", 0);
        TextParameter textParameter = this.textParameterService.findOne(textParameterId);
        if (textParameter == null) {
            textParameter = new TextParameter();
        }
        Precondition.checkSqdsArguement(textParameter != null, "未找到相关参数！");
        List<TextHouseHoldAndArea> textHouseHoldAndAreaList = this.textHouseHoldAndAreaService.getTextHouseHoldAndAreaListByTextParameterId(textParameter.getId());
        modelMap.addAttribute("textParameter", textParameter);
        modelMap.addAttribute("textHouseHoldAndAreaList", textHouseHoldAndAreaList);
    }

    /**
     * 参数配置数据保存
     *
     * @param request
     * @return
     * @throws AjaxException
     */
    @RequestMapping("dataFormSave")
    @ResponseBody
    @RequiresPermissions("textParameter:dataFormSave")
    public synchronized Response dataFormSave(HttpServletRequest request) throws AjaxException {
        Response response = this.textParameterService.dataFormSave(request);
        return response;
    }


    /**
     * 删除参数配置
     *
     * @param request
     * @return
     */
    @RequestMapping("delParameterById")
    @ResponseBody
    @RequiresPermissions("textParameter:delParameterById")
    public synchronized Response delParameterById(HttpServletRequest request) {
        int textParameterId = ParamUtils.getInt(request, "textParameterId", 0);
        TextParameter textParameter = this.textParameterService.findOne(textParameterId);
        Precondition.checkAjaxArguement(textParameter != null, "1111", "数据错误！");
        try {
            //先删除参数配置面积区间信息
            this.textHouseHoldAndAreaService.deleteTextHouseHoldAndAreaByTextParameterId(textParameter.getId());
            this.textParameterService.delete(textParameter.getId());
        } catch (Exception e) {
            Precondition.checkAjaxArguement(false, "9999", "该参数配置已使用，无法删除");
        }
        return new Response("0001", "操作成功");
    }

}
